<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>stringbuffer.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_string_stringbuffer.js.html">stringbuffer.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2006 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Utility for fast string concatenation.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.provide(&#39;goog.string.StringBuffer&#39;);
<a name="line20"></a>
<a name="line21"></a>goog.require(&#39;goog.userAgent.jscript&#39;);
<a name="line22"></a>
<a name="line23"></a>/**
<a name="line24"></a> * Utility class to facilitate much faster string concatenation in IE,
<a name="line25"></a> * using Array.join() rather than the &#39;+&#39; operator.  For other browsers
<a name="line26"></a> * we simply use the &#39;+&#39; operator.
<a name="line27"></a> *
<a name="line28"></a> * @param {Object|number|string|boolean} opt_a1 Optional first initial item
<a name="line29"></a> *     to append.
<a name="line30"></a> * @param {Object|number|string|boolean} var_args Other initial items to
<a name="line31"></a> *     append, e.g., new goog.string.StringBuffer(&#39;foo&#39;, &#39;bar&#39;).
<a name="line32"></a> * @constructor
<a name="line33"></a> */
<a name="line34"></a>goog.string.StringBuffer = function(opt_a1, var_args) {
<a name="line35"></a>  /**
<a name="line36"></a>   * Internal buffer for the string to be concatenated.
<a name="line37"></a>   * @type {string|Array}
<a name="line38"></a>   * @private
<a name="line39"></a>   */
<a name="line40"></a>  this.buffer_ = goog.userAgent.jscript.HAS_JSCRIPT ? [] : &#39;&#39;;
<a name="line41"></a>
<a name="line42"></a>  if (opt_a1 != null) {
<a name="line43"></a>    this.append.apply(this, arguments);
<a name="line44"></a>  }
<a name="line45"></a>};
<a name="line46"></a>
<a name="line47"></a>
<a name="line48"></a>/**
<a name="line49"></a> * Sets the contents of the string buffer object, replacing what&#39;s currently
<a name="line50"></a> * there.
<a name="line51"></a> *
<a name="line52"></a> * @param {string} s String to set.
<a name="line53"></a> */
<a name="line54"></a>goog.string.StringBuffer.prototype.set = function(s) {
<a name="line55"></a>  this.clear();
<a name="line56"></a>  this.append(s);
<a name="line57"></a>};
<a name="line58"></a>
<a name="line59"></a>
<a name="line60"></a>if (goog.userAgent.jscript.HAS_JSCRIPT) {
<a name="line61"></a>  /**
<a name="line62"></a>   * Length of internal buffer (faster than calling buffer_.length).
<a name="line63"></a>   * Only used if buffer_ is an array.
<a name="line64"></a>   * @type {number}
<a name="line65"></a>   * @private
<a name="line66"></a>   */
<a name="line67"></a>  goog.string.StringBuffer.prototype.bufferLength_ = 0;
<a name="line68"></a>
<a name="line69"></a>  /**
<a name="line70"></a>   * Appends one or more items to the buffer.
<a name="line71"></a>   *
<a name="line72"></a>   * Calling this with null, undefined, or empty arguments is an error.
<a name="line73"></a>   *
<a name="line74"></a>   * @param {Object|number|string|boolean} a1 Required first string.
<a name="line75"></a>   * @param {Object|number|string|boolean} opt_a2 Optional second string.
<a name="line76"></a>   * @param {Object|number|string|boolean} var_args Other items to append,
<a name="line77"></a>   *     e.g., sb.append(&#39;foo&#39;, &#39;bar&#39;, &#39;baz&#39;).
<a name="line78"></a>   * @return {goog.string.StringBuffer} This same StringBuffer object.
<a name="line79"></a>   */
<a name="line80"></a>  goog.string.StringBuffer.prototype.append = function(a1, opt_a2, var_args) {
<a name="line81"></a>    // IE version.
<a name="line82"></a>    if (!COMPILED &amp;&amp; !goog.isDef(a1)) {
<a name="line83"></a>      throw Error(&#39;Cannot call StringBuffer.append with zero arguments.&#39;);
<a name="line84"></a>    }
<a name="line85"></a>
<a name="line86"></a>    if (opt_a2 == null) { // second argument is undefined (null == undefined)
<a name="line87"></a>      // Array assignment is 2x faster than Array push.  Also, use a1
<a name="line88"></a>      // directly to avoid arguments instantiation, another 2x improvement.
<a name="line89"></a>      this.buffer_[this.bufferLength_++] = a1;
<a name="line90"></a>    } else {
<a name="line91"></a>      this.buffer_.push.apply(/** @type {Array} */ (this.buffer_), arguments);
<a name="line92"></a>      this.bufferLength_ = this.buffer_.length;
<a name="line93"></a>    }
<a name="line94"></a>    return this;
<a name="line95"></a>  };
<a name="line96"></a>} else {
<a name="line97"></a>
<a name="line98"></a>  /**
<a name="line99"></a>   * Appends one or more items to the buffer.
<a name="line100"></a>   *
<a name="line101"></a>   * Calling this with null, undefined, or empty arguments is an error.
<a name="line102"></a>   *
<a name="line103"></a>   * @param {Object|number|string|boolean} a1 Required first string.
<a name="line104"></a>   * @param {Object|number|string|boolean} opt_a2 Optional second string.
<a name="line105"></a>   * @param {Object|number|string|boolean} var_args Other items to append,
<a name="line106"></a>   *     e.g., sb.append(&#39;foo&#39;, &#39;bar&#39;, &#39;baz&#39;).
<a name="line107"></a>   * @return {goog.string.StringBuffer} This same StringBuffer object.
<a name="line108"></a>   * @suppress {duplicate}
<a name="line109"></a>   */
<a name="line110"></a>  goog.string.StringBuffer.prototype.append = function(a1, opt_a2, var_args) {
<a name="line111"></a>    // W3 version.
<a name="line112"></a>    if (!COMPILED &amp;&amp; !goog.isDef(a1)) {
<a name="line113"></a>      throw Error(&#39;Cannot call StringBuffer.append with zero arguments.&#39;);
<a name="line114"></a>    }
<a name="line115"></a>
<a name="line116"></a>    // Use a1 directly to avoid arguments instantiation for single-arg case.
<a name="line117"></a>    this.buffer_ += a1;
<a name="line118"></a>    if (opt_a2 != null) { // second argument is undefined (null == undefined)
<a name="line119"></a>      for (var i = 1; i &lt; arguments.length; i++) {
<a name="line120"></a>        this.buffer_ += arguments[i];
<a name="line121"></a>      }
<a name="line122"></a>    }
<a name="line123"></a>    return this;
<a name="line124"></a>  };
<a name="line125"></a>}
<a name="line126"></a>
<a name="line127"></a>
<a name="line128"></a>/**
<a name="line129"></a> * Clears the internal buffer.
<a name="line130"></a> */
<a name="line131"></a>goog.string.StringBuffer.prototype.clear = function() {
<a name="line132"></a>  if (goog.userAgent.jscript.HAS_JSCRIPT) {
<a name="line133"></a>     this.buffer_.length = 0;  // Reuse the array to avoid creating new object.
<a name="line134"></a>     this.bufferLength_ = 0;
<a name="line135"></a>   } else {
<a name="line136"></a>     this.buffer_ = &#39;&#39;;
<a name="line137"></a>   }
<a name="line138"></a>};
<a name="line139"></a>
<a name="line140"></a>
<a name="line141"></a>/**
<a name="line142"></a> * Returns the length of the current contents of the buffer.  In IE, this is
<a name="line143"></a> * O(n) where n = number of appends, so to avoid quadratic behavior, do not call
<a name="line144"></a> * this after every append.
<a name="line145"></a> *
<a name="line146"></a> * @return {number} the length of the current contents of the buffer.
<a name="line147"></a> */
<a name="line148"></a>goog.string.StringBuffer.prototype.getLength = function() {
<a name="line149"></a>   return this.toString().length;
<a name="line150"></a>};
<a name="line151"></a>
<a name="line152"></a>
<a name="line153"></a>/**
<a name="line154"></a> * Returns the concatenated string.
<a name="line155"></a> *
<a name="line156"></a> * @return {string} The concatenated string.
<a name="line157"></a> */
<a name="line158"></a>goog.string.StringBuffer.prototype.toString = function() {
<a name="line159"></a>  if (goog.userAgent.jscript.HAS_JSCRIPT) {
<a name="line160"></a>    var str = this.buffer_.join(&#39;&#39;);
<a name="line161"></a>    // Given a string with the entire contents, simplify the StringBuffer by
<a name="line162"></a>    // setting its contents to only be this string, rather than many fragments.
<a name="line163"></a>    this.clear();
<a name="line164"></a>    if (str) {
<a name="line165"></a>      this.append(str);
<a name="line166"></a>    }
<a name="line167"></a>    return str;
<a name="line168"></a>  } else {
<a name="line169"></a>    return /** @type {string} */ (this.buffer_);
<a name="line170"></a>  }
<a name="line171"></a>};
</pre>


</body>
</html>
